# CSU213 Makefile
# Jeff Dlouhy <jdlouhy@ccs.neu.edu>

SOURCE_DIR = .
OUTPUT_DIR = bin/
JARS= ../../libs

JAVA = java
JCC = javac
JFLAGS = -g \
	-source 6 \
	-sourcepath $(SOURCE_DIR)\
	-d $(OUTPUT_DIR)

RM = rm
MKDIR = mkdir -p
SHELL = /bin/bash

CLASSES = PathRoute.java \
	Node.java \
	ILink.java \
	Link.java \
	Graph.java \
	INode.java \
	Node.java \
	Algorithms.java \
	Examples.java \
	IGraphTraverser.java \
	GraphDrawer.java \
	Path.java \
	GraphGenerator.java

# Main Function
MAIN_FUNC = tester.Main

# Le Jars
COLORS_JAR = $(JARS)/colors.jar
DRAW_JAR = $(JARS)/draw.jar
GEO_JAR = $(JARS)/geometry.jar
TESTER_JAR = $(JARS)/tester.jar
JPT_JAR = $(JARS)/jpt.jar

CLASS_PATH = \
	OUTPUT_DIR \
	COLORS_JAR \
	DRAW_JAR \
	GEO_JAR \
	TESTER_JAR \
	JPT_JAR

.SUFFIXES: .java .class

.java.class:
	$(JCC) $(JFLAGS) $*.java

default: classes

classes: $(CLASSES:.java=.class)

run:
	$(JAVA) $(MAIN_FUNC)

clean:
	$(RM) *.class

# Some code borrowed from:
# http://www.makelinux.net/make3/make3-CHP-9-SECT-2.html

export CLASSPATH = $(call build-classpath, $(CLASS_PATH))

# make-directories - Ensure output directory exists.
make-directories = $(shell $(MKDIR) $(OUTPUT_DIR))

$(call make-directories)

all_javas = $(OUTPUT_DIR)/

# compile - Compile the source
.PHONY: compile
compile: $(all_javas)
	$(JCC) $(JFLAGS) @$<

# all_javas - Gather source file list
.INTERMEDIATE: $(all_javas)
$(all_javas):
	$(FIND) $(SOURCE_DIR) -name '*.java' > $@

# $(call build-classpath, variable-list)
define build-classpath
$(strip                                         \
  $(patsubst :%,%,                              \
    $(subst : ,:,                               \
      $(strip                                   \
        $(foreach j,$1,$(call get-file,$j):)))))
endef

# $(call get-file, variable-name)
define get-file
  $(strip                                       \
    $($1)                                       \
    $(if $(call file-exists-eval,$1),,          \
      $(warning The file referenced by variable \
                '$1' ($($1)) cannot be found)))
endef

# $(call file-exists-eval, variable-name)
define file-exists-eval
  $(strip                                       \
    $(if $($1),,$(warning '$1' has no value))   \
    $(wildcard $($1)))
endef


